package com.ftg.learn.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ftg.learn.datahandle.mapper.SubjectScoreMapper;
import com.ftg.learn.datahandle.mapper.SubjectScoreWashMapper;
import com.ftg.learn.entity.SubjectScoreEntity;
import com.ftg.learn.entity.SubjectScoreWashEntity;
import com.ftg.learn.entity.SysUserEntity;
import com.ftg.learn.excelEntity.SubjectScoreExcelEntitiy;
import com.ftg.learn.sys.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 布衣
 */
@Configuration
@EnableRabbit
@Slf4j
public class RabbitmqConfig {

    @Resource
    ObjectMapper objectMapper;

    @Resource
    SubjectScoreMapper subjectScoreMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SubjectScoreWashMapper subjectScoreWashMapper;

    @Bean
    public Queue subjectScoreQueue() {
        return new Queue("subjectScoreQueue", false, false, false, null);
    }

    @Bean
    public Queue subjectScoreQueueWash() {
        return new Queue("subjectScoreQueueWash", false, false, false, null);
    }

    @RabbitListener(queues = "subjectScoreQueue")
    public void process(String message) {
        try {
            List<SubjectScoreExcelEntitiy> list = objectMapper.readValue(message, new com.fasterxml.jackson.core.type.TypeReference<List<SubjectScoreExcelEntitiy>>() {
            });
            list.forEach(item -> {
                QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("sno", item.getStuNo());
                queryWrapper.eq("realname", item.getName());
                SysUserEntity sysUserEntity = sysUserMapper.selectOne(queryWrapper);
                SubjectScoreEntity subjectScoreEntity = new SubjectScoreEntity();
                if (sysUserEntity != null && sysUserEntity.getId() > 0) {
                    System.out.println("查到的某一个同学的" + sysUserEntity.getId());
                    subjectScoreEntity.setUserId(sysUserEntity.getId());
                    subjectScoreEntity.setSubjectId(1234);
                    subjectScoreEntity.setNormalScore(item.getNormalScore());
                    subjectScoreEntity.setTrainScore(item.getTrainScore());
                    subjectScoreEntity.setLastScore(item.getLastScore());
                    subjectScoreEntity.setTotalScore(item.getTotalScore());

                    //list
                    subjectScoreMapper.insert(subjectScoreEntity);
                } else {
                    //错误不存在，应该有其它处理
                    System.out.println(item + "id不存在");
                }
            });
        } catch (JsonProcessingException e) {
            log.error("json转换异常:{}", e.getMessage());
        }
    }


    @RabbitListener(queues = "subjectScoreQueueWash")
    public void processWash(String message) {
        try {
            List<SubjectScoreExcelEntitiy> list = objectMapper.readValue(message, new com.fasterxml.jackson.core.type.TypeReference<List<SubjectScoreExcelEntitiy>>() {
            });
            List<SubjectScoreWashEntity> collect = list.stream().map(item -> {
                SubjectScoreWashEntity subjectScoreEntity = new SubjectScoreWashEntity();
                subjectScoreEntity.setClassName(item.getClassName());
                subjectScoreEntity.setStuNo(item.getStuNo());
                subjectScoreEntity.setName(item.getName());
                subjectScoreEntity.setNormalScore(item.getNormalScore());
                subjectScoreEntity.setTrainScore(item.getTrainScore());
                subjectScoreEntity.setLastScore(item.getLastScore());
                subjectScoreEntity.setTotalScore(item.getTotalScore());
                return subjectScoreEntity;
            }).collect(Collectors.toList());
            //批量添加
            subjectScoreWashMapper.insert(collect);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }
}
